import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Put,
  Query,
  UseGuards,
  HttpCode,
  HttpStatus,
  Req,
} from '@nestjs/common';
import { TaskTemplatesService } from './task-templates.service';
import { CreateTaskTemplateDto } from './dto/create-task-template.dto';
import { UpdateTaskTemplateDto } from './dto/update-task-template.dto';
import { TaskTemplateDto } from './dto/task-template.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole } from '../common/enums';

@ApiTags('Task Template')
@Controller('task-templates')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
export class TaskTemplatesController {
  constructor(private readonly taskTemplatesService: TaskTemplatesService) {}

  @Post()
  @ApiOperation({ summary: 'Create a new task template' })
  @ApiResponse({ status: 201, description: 'Template created successfully', type: TaskTemplateDto })
  async create(
    @Body() createTaskTemplateDto: CreateTaskTemplateDto,
    @Req() req,
  ): Promise<TaskTemplateDto> {
    const userId = req.user.sub || req.user.userId;
    return this.taskTemplatesService.create(createTaskTemplateDto, userId);
  }

  @Get()
  @ApiOperation({ summary: 'Get all task templates' })
  @ApiQuery({ name: 'title', required: false, description: 'Filter by title (partial match)' })
  @ApiResponse({ status: 200, description: 'Return all templates', type: [TaskTemplateDto] })
  async findAll(@Query('title') title?: string): Promise<TaskTemplateDto[]> {
    return this.taskTemplatesService.findAll(title);
  }

  @Get(':templateId')
  @ApiOperation({ summary: 'Get a task template by ID' })
  @ApiParam({ name: 'templateId', description: 'Task template ID' })
  @ApiResponse({ status: 200, description: 'Return the template', type: TaskTemplateDto })
  @ApiResponse({ status: 404, description: 'Template not found' })
  async findOne(@Param('templateId') templateId: string): Promise<TaskTemplateDto> {
    return this.taskTemplatesService.findOne(templateId);
  }

  @Put(':templateId')
  @ApiOperation({ summary: 'Update a task template' })
  @ApiParam({ name: 'templateId', description: 'Task template ID' })
  @ApiResponse({ status: 200, description: 'Template updated successfully', type: TaskTemplateDto })
  @ApiResponse({ status: 404, description: 'Template not found' })
  async update(
    @Param('templateId') templateId: string,
    @Body() updateTaskTemplateDto: UpdateTaskTemplateDto,
  ): Promise<TaskTemplateDto> {
    return this.taskTemplatesService.update(templateId, updateTaskTemplateDto);
  }

  @Delete(':templateId')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete a task template (soft delete)' })
  @ApiParam({ name: 'templateId', description: 'Task template ID' })
  @ApiResponse({ status: 204, description: 'Template deleted successfully' })
  @ApiResponse({ status: 404, description: 'Template not found' })
  async remove(@Param('templateId') templateId: string): Promise<void> {
    return this.taskTemplatesService.remove(templateId);
  }
}
